package com.softstao.guoyu.ui.fragment;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.softstao.guoyu.R;
import com.softstao.guoyu.base.BaseFragment;
import com.softstao.guoyu.global.SharePreferenceManager;
import com.softstao.guoyu.model.me.ProvinceRebate;
import com.softstao.guoyu.mvp.interactor.me.RebateInteractor;
import com.softstao.guoyu.mvp.presenter.me.RebatePresenter;
import com.softstao.guoyu.mvp.viewer.me.ProvinceRebateViewer;
import com.softstao.guoyu.ui.activity.sale.ProvinceRebateDetailsActivity;
import com.softstao.softstaolibrary.library.utils.LZUtils;
import com.softstao.softstaolibrary.library.widget.CustomScrollerView;
import com.wangjie.androidinject.annotation.annotations.mvp.AIPresenter;

import java.util.Calendar;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by vaikus on 2017/7/20.
 */

public class    ProvinceRebateFragment extends BaseFragment implements ProvinceRebateViewer {

    @AIPresenter(presenter = RebatePresenter.class, interactor = RebateInteractor.class)
    RebatePresenter presenter;

    @BindView(R.id.amount)
    TextView amount;
    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.detail_time)
    TextView detailTime;
    @BindView(R.id.one_level_rebate)
    TextView oneLevelRebate;
    @BindView(R.id.two_level_rebate)
    TextView twoLevelRebate;
    //    @BindView(R.id.team_level_rebate)
//    TextView teamLevelRebate;
    @BindView(R.id.scroll_view)
    CustomScrollerView scrollView;
    @BindView(R.id.two_level_completion)
    ImageView twoLevelCompletion;
    @BindView(R.id.is_complete_view)
    ImageView isCompleteView;
    //    @BindView(R.id.team_level_completion)
//    TextView teamLevelCompletion;
    @BindView(R.id.loading)
    RelativeLayout loading;
    @BindView(R.id.two_standard)
    TextView twoStandard;
    private int year, month;
    private Calendar calendar = Calendar.getInstance();
    private ProvinceRebate provinceRebate;

    @Override
    public int _ContentView() {
        return R.layout.fragment_rebate_province;
    }

    @Override
    public void initView() {
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH) + 1;
        time.setText(year + "年" + month + "月");
        detailTime.setText(year + "年" + month + "月返利明细");
    }

    private void initData() {
        amount.setText(LZUtils.priceFormat(provinceRebate.getAmount()) + "元");
        oneLevelRebate.setText(LZUtils.priceFormat(provinceRebate.getOneLevelRebate()) + "元");
        twoLevelRebate.setText(LZUtils.priceFormat(provinceRebate.getTwoLevelRebate()) + "元");
//        teamLevelRebate.setText(LZUtils.priceFormat(provinceRebate.getTeamLevelRebate()) + "元");
        if (provinceRebate.getTwoLevelStandard() != null) {
            Float f = null;
            if (TextUtils.isEmpty(provinceRebate.getTwoLevelStandard().getCompletionQuantity())) {
                f = Float.valueOf(0);
            } else {
                f = new Float(String.valueOf(provinceRebate.getTwoLevelStandard().getCompletionQuantity().substring(0, provinceRebate.getTwoLevelStandard().getCompletionQuantity().indexOf("%")))) / 100;
            }
            if (f > 0) {
                twoLevelCompletion.setImageResource(R.mipmap.yes_ic_pink);
            } else {
                twoLevelCompletion.setImageResource(R.mipmap.no_ic_pink);
            }
            if (provinceRebate.getTwoLevelStandard().getIsCompleted() == 0) {
                isCompleteView.setImageResource(R.mipmap.no_ic_pink);
            } else {
                isCompleteView.setImageResource(R.mipmap.yes_ic_pink);
            }
        } else {
            twoLevelCompletion.setImageResource(R.mipmap.no_ic_pink);
            isCompleteView.setImageResource(R.mipmap.no_ic_pink);
        }
        if (provinceRebate.getTwoIstandard() == 0) {
            twoStandard.setText("不达标");
            twoStandard.setTextColor(getResources().getColor(R.color.font_green));
            twoStandard.setBackground(getResources().getDrawable(R.drawable.green_border_line));
        } else {
            twoStandard.setText("达标");
            twoStandard.setTextColor(getResources().getColor(R.color.font_pink));
            twoStandard.setBackground(getResources().getDrawable(R.drawable.pink_border_line));
        }
//        if (provinceRebate.getTeamLevelStandard()!=null){
//            teamLevelCompletion.setText(provinceRebate.getTeamLevelStandard().getCompletionQuantity());
//        }else {
//            teamLevelCompletion.setText("0%");
//        }
    }

    @Override
    public void onResume() {
        super.onResume();
        findProvinceRebate();
    }

    @OnClick({R.id.left, R.id.right, R.id.one_level_rebate_view, R.id.two_level_rebate_view})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.left:
                month--;
                if (month < 1) {
                    year--;
                    month = 12;
                }
                time.setText(year + "年" + month + "月");
                detailTime.setText(year + "年" + month + "月返利明细");
                findProvinceRebate();
                break;
            case R.id.right:
                month++;
                if (month > 12) {
                    year++;
                    month = 1;
                }
                time.setText(year + "年" + month + "月");
                detailTime.setText(year + "年" + month + "月返利明细");
                findProvinceRebate();
                break;
            case R.id.one_level_rebate_view:
                Intent oneIntent = new Intent(mContext, ProvinceRebateDetailsActivity.class);
                oneIntent.putExtra("method", "oneLevel");
                oneIntent.putExtra("time", time.getText());
                startActivity(oneIntent);
                break;
            case R.id.two_level_rebate_view:
                Intent twoIntent = new Intent(mContext, ProvinceRebateDetailsActivity.class);
                twoIntent.putExtra("method", "twoLevel");
                twoIntent.putExtra("time", time.getText());
                startActivity(twoIntent);
                break;
        }
    }

    @Override
    public void findProvinceRebate() {
        loading.setVisibility(View.VISIBLE);
        presenter.getProvinceRebate(SharePreferenceManager.getInstance().getAgentId(), time.getText().toString().replace("年", "-").replace("月", ""));
//        presenter.getProvinceRebate(17901,time.getText().toString().replace("年", "-").replace("月", ""));
    }

    @Override
    public void getProvinceRebate(ProvinceRebate provinceRebate) {
        loading.setVisibility(View.GONE);
        if (provinceRebate != null) {
            this.provinceRebate = provinceRebate;
            initData();
        }
    }
}
